/* 
 * sections/callback.cpp
 * Written by, Sohail Qayum Malik[sqm@hackers.pk]
 */

/* For debuging purposes, should be gone when done */
#include <iostream>
/*  For debugging purposes ends here */

#include <stdio.h>  
#include "callback.h"

int callback_sections(int argc, ...) { 
 
   int ret = 0;  
   DWORD size = 0;
   PIMAGE_SECTION_HEADER ptr = NULL;   
   va_list VList;
   
   PIMAGE_DOS_HEADER dosheader = NULL;
   /* it is PIMAGE_NT_HEADERS and not singular for a reason... */
   PIMAGE_NT_HEADERS ntheaders = NULL;
   /* ntheaders has fileheader as well */
   PIMAGE_FILE_HEADER fileheader = NULL;
   
   va_start(VList, argc);
      
   dosheader = va_arg(VList, PIMAGE_DOS_HEADER);
   ntheaders = va_arg(VList, PIMAGE_NT_HEADERS);
   size = va_arg(VList, DWORD);
   ptr = va_arg(VList, PIMAGE_SECTION_HEADER);
            
   va_end(VList);
   
   fileheader = &ntheaders->FileHeader;
   
   if ( size == fileheader->NumberOfSections ) {
   
      std::cout<<std::endl<<"IMAGE_SECTION_HEADERs( = "<<size<<")"<<std::endl;
	  std::cout<<"------------------------------------------"<<std::endl;
   
      for ( DWORD i = 0; i < size; i++ ) {
	  
	     std::cout<<"IMAGE_SECTION_HEADER("<<i + 1<<")"<<std::endl;
		 std::cout<<"---------------------------------------"<<std::endl;
	     std::cout<<"IMAGE_SECTION_HEADER::Name[] = ";
	  
	     for ( DWORD j = 0; j < IMAGE_SIZEOF_SHORT_NAME; j++ ) {
		 
	        std::cout<<(ptr + i)->Name[j]; 
		 }
		 
		 std::cout<<std::endl;
		 std::cout<<"IMAGE_SECTION_HEADER::Misc::VirtualSize = "<<(ptr + i)->Misc.VirtualSize<<std::endl;
		 std::cout<<std::hex<<"IMAGE_SECTION_HEADER::VirtualAddress = 0x"<<(ptr + i)->VirtualAddress<<std::dec<<std::endl;
		 std::cout<<"IMAGE_SECTION_HEADER::SizeOfRawData = "<<(ptr + i)->SizeOfRawData<<std::endl;
		 std::cout<<std::hex<<"IMAGE_SECTION_HEADER::PointerToRawData = 0x"<<(ptr + i)->PointerToRawData<<std::dec<<std::endl;
		 std::cout<<std::hex<<"IMAGE_SECTION_HEADER::PointerToRelocations = 0x"<<(ptr + i)->PointerToRelocations<<std::dec<<std::endl;
		 std::cout<<std::hex<<"IMAGE_SECTION_HEADER::PointerToLinenumbers = 0x"<<(ptr + i)->PointerToLinenumbers<<std::dec<<std::endl;
		 std::cout<<"IMAGE_SECTION_HEADER::NumberOfRelocations = "<<(ptr + i)->NumberOfRelocations<<std::endl;
		 std::cout<<"IMAGE_SECTION_HEADER::NumberOfLinenumbers = "<<(ptr + i)->NumberOfLinenumbers<<std::endl;
         std::cout<<std::hex<<"IMAGE_SECTION_HEADER::Characteristics = 0x"<<(ptr + i)->Characteristics<<std::dec<<std::endl;		 
		 
		 std::cout<<std::endl;
	  }
            
   } else {
   
      std::cout<<"PIMAGE_NT_HEADERS::PIMAGE_FILE_HEADER::NumberOfSections mismatch"<<std::endl; 
   }
   
        
   return ret;
}	
